package tut.collection.dataStructure.compareableVsComparator.sortHashMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MySortListOfHashMap {
	public static final String KEY = "id";
	public static final String VALUE = "value";

	public static void main(String[] args) {
		List<Map> list = new ArrayList<Map>();
		list.add(makeMap("002", "val02"));
		list.add(makeMap("003", "val03"));
		list.add(makeMap("000", "val00"));

		printListOfMap(list);
		Collections.sort(list,(new MySortListOfHashMap()).new HashMapComparetor());
		printListOfMap(list);
	}

	public static Map makeMap(String key, String value) {
		Map m = new HashMap<String, String>();
		m.put(KEY, key);
		m.put(VALUE, value);
		return m;
	}

	public static void printListOfMap(List<Map> list){
		System.out.println("-------------");
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Map map = (Map) iterator.next();
			System.out.println(map.get(KEY));
			System.out.println(map.get(VALUE));
		}
		System.out.println("-------------");
	}

	public class HashMapComparetor implements Comparator<Map> {

		public int compare(Map m1, Map m2){
			String key1 = (String) m1.get(KEY);
			String key2 = (String) m2.get(KEY);
			return key1.compareTo(key2);
		}

	}
}
